package com.yonyou.iuap.controller.pc;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.FileUtils;
import org.apache.commons.net.ftp.FTPClient;
import org.ocpsoft.prettytime.PrettyTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yonyou.iuap.auth.session.SessionManager;
import com.yonyou.iuap.pub.FtpTask;
import com.yonyou.iuap.pub.JsonUtils;
import com.yonyou.iuap.shell.entity.KmPublishEntity;
import com.yonyou.iuap.shell.entity.exp.KmpublishPoJo;
import com.yonyou.iuap.shell.entity.exp.KmpublishfacomPoJo;
import com.yonyou.iuap.shell.pub.RequestDto;
import com.yonyou.iuap.shell.pub.ResultDto;
import com.yonyou.iuap.shell.service.intf.IFtpService;
import com.yonyou.iuap.shell.service.intf.IKmPublishService;
import com.yonyou.iuap.shell.util.ShellUtils;
/**
 * 查询最新知识和最热知识
 * autoc shiwenqiang
 * **/
@Controller
@RequestMapping(value="/PCpublish")
public class PCKmPublishController {
	private final Logger logger = (Logger) LoggerFactory.getLogger(getClass());
	private long start,end;
		@Autowired
		private IKmPublishService kmPublishServiceImpl; 
		@Autowired
		private SessionManager sessionManager;			
		@Autowired
		private IFtpService ftpService;

		private int pageindex_size = 15;
		/**
		 * 最新知识
		 * **/
		@RequestMapping(value="/queryRecentKnowledges",method=RequestMethod.POST)
		@ResponseBody
		public String newpublishasc(@RequestBody Map map,HttpServletRequest request,HttpServletResponse response) throws ParseException{
			start=System.currentTimeMillis(); 
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:m:s");
			StringBuffer str=new StringBuffer();
			str.append(" sealmark='Y'  ");
			RequestDto reqDto = new RequestDto();
			reqDto.setParams(str.toString());
			reqDto.setPageNum(0);
			reqDto.setPageSize(this.pageindex_size);
			ResultDto resDto = kmPublishServiceImpl.selectpublishtimeasc(reqDto);
			List listvo =	(List) resDto.getData();
			for(int i=0;i<listvo.size();i++){
				KmPublishEntity kk=(KmPublishEntity)listvo.get(i);
				if(kk.getPublish_time()!=null){
					String aa=kk.getPublish_time();
					Date date = format.parse(aa);
					Date now = new Date();
					PrettyTime t = new PrettyTime(now);
					kk.setPublish_time(t.format(date));
				}
				
			}
			ResultDto rsDto = new ResultDto();
			rsDto.setStatus(0);
			rsDto.setMessage("查询成功");
			rsDto.setData(listvo);
			end=System.currentTimeMillis();
			logger.info("queryRecentKnowledges接口调用时间"+String.valueOf(end-start));
			return JsonUtils.toJsonStr(rsDto);
	}
		
		/**
		 * 知识数量统计
		 * */
		@RequestMapping(value="queryKnowledgeCount", method=RequestMethod.POST)
		public @ResponseBody String queryKmpublishPoJoLike(@RequestBody Map map,HttpServletRequest request,HttpServletResponse response){
			start=System.currentTimeMillis();
			List<KmpublishPoJo> pojo=kmPublishServiceImpl.CountKmpublishPoJo();
			ResultDto rsDto = new ResultDto();
			rsDto.setStatus(0);
			rsDto.setMessage("查询成功");
			rsDto.setData(pojo);
			end=System.currentTimeMillis();
			logger.info("queryKnowledgeCount接口调用时间"+String.valueOf(end-start));
			return JsonUtils.toJsonStr(rsDto);
		}	
		
		/**
		 * 
		 * 查询最热知识
		 * @throws ParseException 
		 * ****/
		@RequestMapping(value="queryKmpublishfavoaritecomment",method=RequestMethod.POST)
		@ResponseBody
		public String queryKmpublishfavoaritecomment(@RequestBody Map map,HttpServletRequest request,HttpServletResponse response) throws ParseException{
			start=System.currentTimeMillis();
			ResultDto resDto = kmPublishServiceImpl.queryKmpublishfavoaritecomment();
			 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:m:s");
			if(resDto.getStatus()==0){
				List<KmpublishfacomPoJo> listvo = (List<KmpublishfacomPoJo>) resDto.getData();
				for(int i=0;i<listvo.size();i++){
					KmpublishfacomPoJo kk=(KmpublishfacomPoJo)listvo.get(i);
					String aa=kk.getPublish_time();
					  Date date = format.parse(aa);
				        Date now = new Date();
				        PrettyTime t = new PrettyTime(now);
				        kk.setPublish_time(t.format(date));
				}
			}
			end=System.currentTimeMillis();
			logger.info("queryKmpublishfavoaritecomment接口调用时间"+String.valueOf(end-start));
			return JsonUtils.toJsonStr(resDto);
		}
		
		
		/**
		 * 条件查询知识库列表 （9988分页 ） -------------- 
		 * @param parMap
		 * @param request
		 * @param response
		 * @return
		 */
		@RequestMapping(value="queryKmpublishPoJoLikePage", method=RequestMethod.POST)
		@ResponseBody 
		public  String queryKmpublishPoJoLikePage(@RequestBody Map map , HttpServletRequest request,HttpServletResponse response){
			start=System.currentTimeMillis();
			ResultDto rsDto = new ResultDto();
			rsDto=kmPublishServiceImpl.queryKmpublishPoJoLikePc(map);
			List<KmpublishPoJo> listvo = (List<KmpublishPoJo>) rsDto.getData();
			if(listvo != null && listvo.size() > 0){
				rsDto.setStatus(0);
				rsDto.setMessage("数据查询成功!");
			}else {
				rsDto.setMessage("暂无数据");
			}
			end=System.currentTimeMillis();
			logger.info("queryKmpublishPoJoLikePage接口调用时间"+String.valueOf(end-start));
			return JsonUtils.toJsonStr(rsDto);
	
		}
		
		/**
		 * 知识库列表 修改记录 封存标志     是否启用      Y启用   N禁用
		 * @param parMap
		 * @param request
		 * @param response
		 * @return
		 */
		@RequestMapping(value="updateSealmark", method=RequestMethod.POST)
		@ResponseBody 
		public  String updateSealmark(@RequestBody Map map , HttpServletRequest request,HttpServletResponse response){
			start=System.currentTimeMillis();
			ResultDto rsDto = new ResultDto();
			rsDto=kmPublishServiceImpl.updateSealmark(map);
			end=System.currentTimeMillis();
			logger.info("updateSealmark接口调用时间"+String.valueOf(end-start));
			return JsonUtils.toJsonStr(rsDto);
	
		}
		
		/**
		 * 获取知识详情 （知识修改前拉取当前要修改的知识信息）
		 * @param parMap.pk_km_publish  知识id
		 * @param request
		 * @param response
		 * @return
		 */
		@RequestMapping(value="updatePublishFront", method=RequestMethod.POST)
		@ResponseBody 
		public  String updatePublishFront(@RequestBody Map map , HttpServletRequest request,HttpServletResponse response){
			start=System.currentTimeMillis();
			ResultDto rsDto = new ResultDto();
			rsDto=kmPublishServiceImpl.updatePublishFront(map);
			end=System.currentTimeMillis();
			logger.info("updatePublishFront接口调用时间"+String.valueOf(end-start));
			return JsonUtils.toJsonStr(rsDto);
	
		}
		
		/**
		 * 删除发布的知识
		 * @param parMap.pk_km_publish  知识id
		 * @param request
		 * @param response
		 * @return
		 */
		@RequestMapping(value="delPublishByid", method=RequestMethod.POST)
		@ResponseBody 
		public  String delPublishByid(@RequestBody Map map , HttpServletRequest request,HttpServletResponse response){
			start=System.currentTimeMillis();
			ResultDto rsDto = new ResultDto();
			rsDto=kmPublishServiceImpl.delPublishByid(map);
			end=System.currentTimeMillis();
			logger.info("delPublishByid接口调用时间"+String.valueOf(end-start));
			return JsonUtils.toJsonStr(rsDto);
	
		}
		
		/**
		 * 新增知识
		 * @param request
		 * @param response
		 * @return
		 */					   
		@RequestMapping(value="insertPublishEntity", method=RequestMethod.POST)
		@ResponseBody 
		public  String insertPublishEntity(@RequestBody Map map , HttpServletRequest request,HttpServletResponse response){
			start=System.currentTimeMillis();
			uploadToFtp(request);
			ResultDto rsDto = new ResultDto();
			rsDto=kmPublishServiceImpl.insertPublishEntity(map);
			end=System.currentTimeMillis();
			logger.info("insertPublishEntity接口调用时间"+String.valueOf(end-start));
			return JsonUtils.toJsonStr(rsDto);
	
		}
		
		/**
		 * 异步将上传到临时文件夹文件传到FTP
		 */
		private void uploadToFtp(HttpServletRequest request) {
			File file=new File(request.getRealPath("")+"\\temp\\"+ShellUtils.getRemoteHost(request));
			FtpTask task=new FtpTask(ftpService, file);
			new Thread(task).start();
		}

		/**
		 * 修改知识
		 * @param request
		 * @param response
		 * @return
		 */
		@RequestMapping(value="updatePublishEntity", method=RequestMethod.POST)
		@ResponseBody 
		public  String updatePublishEntity(@RequestBody Map map , HttpServletRequest request,HttpServletResponse response){
			start=System.currentTimeMillis();
			uploadToFtp(request);
			ResultDto rsDto = new ResultDto();
			rsDto=kmPublishServiceImpl.updatePublishEntity(map);
			end=System.currentTimeMillis();
			logger.info("updatePublishEntity接口调用时间"+String.valueOf(end-start));
			return JsonUtils.toJsonStr(rsDto);
	
		}

		
}
